//
//  CC_Basic_View.m
//  HimalayaMountain
//
//  Created by dllo on 16/7/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CC_Basic_View.h"
#import "CCBaseCollectionView.h"
#import "CC_NavigationCollec_CollectionViewCell.h"
#import "CC_Recommend_CollectionViewCell.h"  // 推荐cell
#import "CC_Classify_CollectionViewCell.h"      // 分类cell
#import "CC_TopList_CollectionViewCell.h"    // 榜单
#import "CC_Anchor_CollectionViewCell.h"  // 主播cell




#define NAVICOLLECCELL @"navigationCollectionCell"
#define CONTENTCELL @"contentCollectionCell"

// 内容大cell
#define FirstCell @"contentRecommendCell"
#define SeondCell @"CC_Classify_CollectionViewCell.h"
#define FouthCELL @"CC_TopList_CollectionViewCell"
#define FIVECELL @"CC_Anchor_CollectionViewCell"


@interface CC_Basic_View ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property(nonatomic, retain) CCBaseCollectionView *navigationCollectionViewOfFind;
@property(nonatomic, retain) CCBaseCollectionView *contentCollectionViewOfFind;

@end




@implementation CC_Basic_View

- (void)dealloc
{  [_arrayOfTitle release];
    [_navigationCollectionViewOfFind release];
    [_contentCollectionViewOfFind release];
    [super dealloc];
}


    
-(instancetype)initWithFrame:(CGRect)frame{
    if ( self = [super initWithFrame:frame]) {
        
        [self createTitleCollectionView];
        [self createContentCollectionView];
    }
    return self;
}

-(void)createTitleCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake((SCREENWIDTH - 6 * SPACED) / 5, 30);
//    flowLayout.minimumLineSpacing = 10;
//    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(SPACED, SPACED, 0, SPACED);
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.navigationCollectionViewOfFind = [[CCBaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 40) collectionViewLayout:flowLayout];
    [self addSubview:_navigationCollectionViewOfFind];
    
    self.navigationCollectionViewOfFind.delegate = self;
    self.navigationCollectionViewOfFind.dataSource = self;
    self.navigationCollectionViewOfFind.showsHorizontalScrollIndicator = NO;

    [self.navigationCollectionViewOfFind registerClass:[CC_NavigationCollec_CollectionViewCell class] forCellWithReuseIdentifier:NAVICOLLECCELL];
    [_navigationCollectionViewOfFind release];
    
    
}

-(void)createContentCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize =CGSizeMake(SBWIDTH, SBHEIGHT);
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    self.contentCollectionViewOfFind = [[CCBaseCollectionView alloc]initWithFrame:CGRectMake(0, 40,SCREENWIDTH, SCREENHEIGHT - 64 - 59) collectionViewLayout:flowLayout];
    

    
    [self addSubview:_contentCollectionViewOfFind];
    
    self.contentCollectionViewOfFind.pagingEnabled = YES;
    
    self.contentCollectionViewOfFind.delegate = self;
    self.contentCollectionViewOfFind.dataSource = self;
    
    
    // 推荐cell
    [self.contentCollectionViewOfFind registerClass:[CC_Recommend_CollectionViewCell class] forCellWithReuseIdentifier:FirstCell];
    
    // 分类cell
    [self.contentCollectionViewOfFind registerClass:[CC_Classify_CollectionViewCell class] forCellWithReuseIdentifier:SeondCell];
    
    // 榜单
    [self.contentCollectionViewOfFind registerClass:[CC_TopList_CollectionViewCell class] forCellWithReuseIdentifier:FouthCELL];
    
    // 主播
    [self.contentCollectionViewOfFind registerClass:[CC_Anchor_CollectionViewCell class] forCellWithReuseIdentifier:FIVECELL];
    
    
    [self.contentCollectionViewOfFind registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CONTENTCELL];
    [flowLayout release];
    [_contentCollectionViewOfFind release];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arrayOfTitle.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.navigationCollectionViewOfFind) {
        CC_NavigationCollec_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NAVICOLLECCELL forIndexPath:indexPath];

        cell.naviModel = [self.arrayOfTitle objectAtIndex:indexPath.row];
        

        return cell;
    }else
    {
        
        if (indexPath.row == 0)
        {   // 第一页导航栏推荐
            
            CC_Recommend_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:FirstCell forIndexPath:indexPath];
            cell.contentView.backgroundColor = [UIColor whiteColor];
            return cell;
            
            
        } else if (indexPath.row== 1)
        {   // 分类 导航栏中的
            CC_Classify_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:SeondCell forIndexPath:indexPath];
            cell.backgroundColor = [UIColor grayColor];
            return cell;
            
        }else if (indexPath.row == 2)
        {
             UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CONTENTCELL forIndexPath:indexPath];
//        cell.backgroundColor = AUTOCOLOR;
            return cell;
            
        }else if (indexPath.row == 3)
        {
            CC_TopList_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:FouthCELL forIndexPath:indexPath];
            cell.backgroundColor = AUTOCOLOR;
            return cell;
            
            
        }else
        { // 第一页导航栏 主播
            CC_Anchor_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:FIVECELL forIndexPath:indexPath];
            cell.contentView.backgroundColor = [UIColor grayColor];
            return cell;
        }
    }

}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (collectionView == self.navigationCollectionViewOfFind) {
        [self.contentCollectionViewOfFind setContentOffset:CGPointMake(indexPath.row * SCREENWIDTH, 0)];
        
        CC_NavigationCollec_CollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
        cell.isSelected = YES;
        
    }
    
    
}

// 滚动内容 导航栏偏移量改变______暂时有问题
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (collectionView == self.navigationCollectionViewOfFind) {
        CC_NavigationCollec_CollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
        cell.isSelected = NO;

    }
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (scrollView == self.contentCollectionViewOfFind) {
        
        NSInteger row = self.contentCollectionViewOfFind.contentOffset.x / self.contentCollectionViewOfFind.frame.size.width;
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
        
        [self.navigationCollectionViewOfFind setContentOffset:CGPointMake(indexPath.row *((SCREENWIDTH - 60) / 5 + 10) , 0)];
        
        [self changeColor:indexPath];
        
    }
    
}

// 改变颜色
-(void)changeColor:(NSIndexPath *)indexPath{
    
    CC_NavigationCollec_CollectionViewCell *cell = [self.navigationCollectionViewOfFind cellForItemAtIndexPath:indexPath];
    cell.isSelected = YES;
    
}

-(void)setArrayOfTitle:(NSArray *)arrayOfTitle{
    
    if (_arrayOfTitle != arrayOfTitle) {
        [_arrayOfTitle release];
        _arrayOfTitle = [arrayOfTitle retain];
    }
    
    [self.navigationCollectionViewOfFind reloadData];
    [self.contentCollectionViewOfFind reloadData];
    
}



@end
